package com.mk.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

import com.alibaba.fastjson.JSON;

public class ListDemo
{
	@Test
	public void test1() {
		List<String> list = new ArrayList<>();
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		list.add("沈钧儒");
		list.add("郑茂才");
		
//		List<String> child = list.subList(1, 5).stream().collect(Collectors.toList());
		List<String> child = new ArrayList<>(list.subList(1, 5));
		child.set(0, "李承乾");
		System.out.println(child.toString());
		System.out.println(list.toString()); 
	}
	
	
	@Test
	public void test2() {
		List<Map<String,Object>> rsImileStone = new ArrayList<>(3);
		
		Map<String,Object> fm = new HashMap<>(4);
		fm.put("projectStage", "项目实施（开发）阶段");
        fm.put("startTime", "T + 1");
        fm.put("endTime", "");
        fm.put("taskDescription", "需求分析，系统设计，开发，单元测试等");
        rsImileStone.add(fm);
        
        Map<String,Object> sm = new HashMap<>(4);
        sm.put("projectStage", "项目实施（测试）阶段");
        sm.put("startTime", "T + 22");
        sm.put("endTime", "");
        sm.put("taskDescription", "系统测试，用户验收测试，培训等");
        rsImileStone.add(sm);
        
        Map<String,Object> tm = new HashMap<>(4);
        tm.put("projectStage", "项目上线阶段");
        tm.put("startTime", "");
        tm.put("endTime", "" );
        tm.put("taskDescription", "项目上线，上线验证等");
        rsImileStone.add(tm);
        
        System.out.println( rsImileStone.toString() );
	}
	

	// 迭代
	@Test
	public void test4() {
		List<String> list = new ArrayList<>(3);
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		
		list.forEach( item->{
			System.out.println(item);
		});
		
	}
	
	@Test
	public void test5() {
		List<String> list = new ArrayList<>(3);
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		
		
		List<String> res = Optional.ofNullable(list).orElse(Collections.emptyList());
		System.out.println(res);
	}
	
	@Test
	public void test6() {
		String[] strArr = {"裂开", "是", "大"};
		System.out.println(strArr.toString()); // [Ljava.lang.String;@53bd815b
		System.out.println(Arrays.toString(strArr)); // [裂开, 是, 大]
		System.out.println(Arrays.deepToString(strArr) ); // [裂开, 是, 大]
		System.out.println(Arrays.toString(strArr).replace("[", "").replace("]", "") ); // 裂开, 是, 大 
	}
	
	@Test
	public void test7() {
		List<String> contentList = 
				Arrays.asList("天气降温[]，温差大", "[每日查房]，注意保暖", "将强陪护员教育");
		System.out.println(JSON.toJSONString(contentList));
		// ["天气降温[]，温差大","[每日查房]，注意保暖","将强陪护员教育"]
	}
	
	@Test
	public void test8() {
		String s="[\"天气降温[]，温差大\",\"[每日查房]，注意保暖\",\"将强陪护员教育\"]";
		List<String> list =  JSON.parseArray(s, String.class);
		list.forEach(System.out::println);
	}
	
	@Test
	public void testConvertToString() {
		List<String> list = new ArrayList<>(4);
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		list.add("贺知章");
		
//		System.out.println(Stream.of(list).toString() ); // java.util.stream.ReferencePipeline$Head@2471cca7
//		System.out.println( list.stream().toString() ); // java.util.stream.ReferencePipeline$Head@2471cca7
		
		System.out.println( list.stream().collect(Collectors.joining("\r\n")) );
	}
	
	@Test
	public void test9() {
//		List<String> list = null;
//		Stream.of(list).filter();
//		String str = Stream.of(/list).collect(Collectors.joining(","));
		List<String> list = new ArrayList<>(4);
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		list.add("贺知章");
		
//		String string = Optional.ofNullable(list).orElseGet(()->Collections.emptyList()).stream().collect(Collectors.joining("\r\n"));
		System.out.println(list.stream().collect(Collectors.joining(",")));
	}
	
	@Test
	public void test10() {
		List<Integer> list = new ArrayList<>();
//		list.add(5);
//		list.add(6);
//		list.add(11);
		
		Integer[] as = list.stream().toArray(Integer[]::new);
		
		System.out.println( Arrays.toString(as) );
	}
	
	@Test
	public void test11() {
		Integer[][] arr = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
		
	}
	
	@Test
	public void test12() {
		Integer[] mon = {4,5,6};
		Stream.of(mon).collect(Collectors.summarizingDouble(null));
	}
	
	@Test
	public void testSubList() {
        List<String> resList = Arrays.asList("0", "1", "2", "3", "4", "5", "6",
                "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
                "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
                "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
                "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
                "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
                "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
                "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
                "78", "79", "80", "81", "82", "83", "84", "85", "86", "87",
                "88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
                "98","99","100");
        resList = new ArrayList<>(resList);
        int pageSize = 5;
        while (!resList.isEmpty()){
            List<String> splitList = resList.subList(0, pageSize < resList.size() ? pageSize : resList.size());
            System.out.println("-----"+splitList);
            // 清除已使用过的数据
            splitList.clear();
        }
        
        System.out.println(resList);
	}
	
	@Test
	public void testSplitList() {
		List<String> resList = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
        resList = new ArrayList<>(resList);
        List<String> result = resList.subList(0, 4);
        System.out.println(result);
        result.clear();
        System.out.println(resList);
        System.out.println(resList.size());
	}
	
	@Test
	public void testSplitList2() {
		List<String> resList = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
        resList = new ArrayList<>(resList);
        
        while(!resList.isEmpty()) {
        	List<String> child = resList.subList(0, resList.size()>5 ? 5:resList.size());
        	System.out.println("子列表: "+child);
        	child.clear();
        }
	}
	
	@Test
	public void testFilter() {
		List<String> list = new ArrayList<>();
		list.add("李九龄");
		list.add("李婵品");
		list.add("张帮昌");
		list.add("沈钧儒");
		list.add("郑茂才");
		
		boolean res = list.stream().anyMatch(t->"帮昌".equals(t));
		System.out.println(res); 
	}
	
}
